if(!requireNamespace("animint2")) install.packages("animint2")
Warning message:
R graphics engine version 16 is not supported by this version of RStudio. The Plots tab will be disabled until a newer version of RStudio is installed.
library(animint2)
Attaching package: ‘animint2’
The following object is masked _by_ ‘.GlobalEnv’:
WorldBank
Translating plot sketches into ggplots
data(WorldBank, package="animint2")
WorldBank$Region <- sub(" (all income levels)", "", WorldBank$region, fixed=TRUE)
tail(WorldBank)
dim(WorldBank)
[1] 11342 16
WorldBank1975 <- subset(WorldBank, year==1975)
head(WorldBank1975)
scatter <- ggplot()+
geom_point(
mapping=aes(x=life.expectancy, y=fertility.rate, color=Region),
data=WorldBank1975)
scatter

animint(scatter)
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(15) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db3659f7a1 at http://127.0.0.1:3474
Multi-layer data visualization (multiple geoms)
WorldBankBefore1975 <- subset(WorldBank, 1970 <= year & year <= 1975)
two.layers <- scatter+
geom_path(aes(
x=life.expectancy,
y=fertility.rate,
color=Region,
group=country),
data=WorldBankBefore1975)
two.layers

(viz.two.layers <- animint(two.layers))
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(13) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db5da0c848 at http://127.0.0.1:3580
viz.two.layers
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(14) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db5611158b at http://127.0.0.1:4136
three.layers <- two.layers+
geom_text(aes(
x=life.expectancy,
y=fertility.rate,
color=Region,
label=country),
data=WorldBank1975)
three.layers

animint(three.layers)
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(12) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db7f62f71a at http://127.0.0.1:5913
Multi-plot data visualization
viz.two.plots <- viz.two.layers
Warning messages:
1: In readChar(file, size, TRUE) : truncating string with embedded nuls
2: In readChar(file, size, TRUE) : truncating string with embedded nuls
3: In readChar(file, size, TRUE) : truncating string with embedded nuls
viz.two.plots$timeSeries <- ggplot()+
geom_line(aes(
x=year,
y=fertility.rate,
color=Region,
group=country),
data=WorldBank)
summary(viz.two.plots)
Length Class Mode
twolayers 9 gganimint list
timeSeries 9 gganimint list
viz.two.plots$twolayers

viz.two.plots$timeSeries

viz.two.plots
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(11) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db6ec6a40 at http://127.0.0.1:6215
Exercise: use animint to create a data viz with three plots, by creating a list with three ggplots.
viz.three.plots <- viz.two.plots
viz.three.plots$population <- ggplot()+
geom_point(aes(
x=year,
y=population,
color=Region),
data=WorldBankBefore1975)
viz.three.plots
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(8) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db63570fe9 at http://127.0.0.1:7342
viz.three.plots$twolayers

viz.three.plots$timeSeries

viz.three.plots$population

viz.three.plots
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(16) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db2488cef6 at http://127.0.0.1:6950
Multi-panel data visualization (facets)
Different geoms in each panel (aligned axes)
add.x.var <- function(df, x.var){
data.frame(df, x.var=factor(x.var, c("life expectancy", "year")))
}
(viz.aligned <- animint(
scatter=ggplot()+
theme_animint(width=600)+
geom_point(aes(x=life.expectancy, y=fertility.rate, color=Region),
data=add.x.var(WorldBank1975, "life expectancy"))+
geom_path(aes(x=life.expectancy, y=fertility.rate, color=Region,
group=country),
data=add.x.var(WorldBankBefore1975, "life expectancy"))+
geom_line(aes(x=year, y=fertility.rate, color=Region, group=country),
data=add.x.var(WorldBank, "year"))+
xlab("")+
facet_grid(. ~ x.var, scales="free")+
theme_bw()+
theme(panel.margin=grid::unit(0, "lines"))
))
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(19) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db4318569d at http://127.0.0.1:3321
viz.aligned$scatter

Same geoms in each panel (compare data subsets)
show.point.list <- list()
show.path.list <- list()
for(show.year in c(1975, 1985, 1995)){
show.point.list[[paste(show.year)]] <- data.frame(
show.year, subset(WorldBank, year==show.year))
show.path.list[[paste(show.year)]] <- data.frame(
show.year, subset(WorldBank, show.year - 5 <= year & year <= show.year))
}
show.point <- do.call(rbind, show.point.list)
show.path <- do.call(rbind, show.path.list)
viz.panels <- list(
scatter=ggplot()+
geom_point(aes(x=life.expectancy, y=fertility.rate, color=Region),
data=show.point)+
geom_path(aes(x=life.expectancy, y=fertility.rate, color=Region,
group=country),
data=show.path)+
facet_grid(. ~ show.year)+
theme_bw()+
theme(panel.margin=grid::unit(0, "lines"))
)
viz.panels$scatter

structure(viz.panels, class="animint")
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
To stop the server, run servr::daemon_stop(21) or restart your R session
Serving the directory /private/var/folders/ny/0zb_vjr53qqb_svqhxcprhtw0000gn/T/Rtmpm522Nz/file156db7ecd385a at http://127.0.0.1:5664
LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKYGBge3J9CmlmKCFyZX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